package com.bw.controller;

import com.bw.utils.R;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author bobo棒
 * @date 2024年07月08日 16:27
 */

@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Value("${fastAddress}")
    String fastAddress;

    @RequestMapping("upload")
    public R upload(MultipartFile file) throws IOException {
        String fileOriginalFilename = file.getOriginalFilename();
        String extname = StringUtils.getFilenameExtension(fileOriginalFilename);

        String fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
                extname, null).getFullPath();
        return R.success(fastAddress+fullPath);
    }
}
